<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General Public License version 2 or later
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

/**
 * HelloWorld Model
 */
class bgModelbg extends JModel
{
     var $_total = null;
     var $_pagination = null;
    
    public function __construct(){
    	parent::__construct();
 
        $mainframe = JFactory::getApplication();
 
        // Get pagination request variables
        $limit = $mainframe->getUserStateFromRequest('com_fractivitive.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        // In case limit has been changed, adjust it
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
 
        $this->setState('limit', 5);
         //$this->setState('limit', 5);
        $this->setState('limitstart', $limitstart);
    }
	function getAll($list = null){

       $qr = "SELECT ACTT_NAME,ACTT_RECR,ACTT_CODE,ACTT_FLAG,ACTT_REMARKS,OCBA_NAME,ACTT_PERIOD_START,ACTT_PERIOD_FINISH,OCBA_CODE,ACTT_NUMBER_OF_PARTICIPANTS,ACTT_RECOMMENDED_DEGREE,PREF_CODE,
                	ACTT_MFAC_CODE_LIST,ACTT_MOBJ_CODE_LIST,ACTT_MCON_CODE_LIST		
                FROM actt LEFT JOIN ocba ON actt.ACTT_OCBA_CODE=ocba.OCBA_CODE
                                    	LEFT JOIN town ON ocba.OCBA_TOWN_CODE=town.TOWN_CODE
                                    	LEFT JOIN pref ON town.TOWN_PREF_CODE=pref.PREF_CODE

              ";      
        $where = array();
        $where[] = "ACTT_WEBF = 1";

        //search PREF     
          if(!empty($list['PREF'])){
          $where[] = "PREF_CODE = $list[PREF]";
         }
        //search name leader     
          if(!empty($list['ACTT_NAME'])){
          $where[] = "ACTT_NAME LIKE '%$list[ACTT_NAME]%'";
         }
         //search RECOMMENDED DEGREE
        if(($list['PEDE']) !=0 ){
                   $where[] = "ACTT_RECOMMENDED_DEGREE =$list[PEDE]";  
        }
        //search NUMBER OF PARTICIPANTS
           if(($list['ACTT_FNUM'] != null) && ($list['ACTT_TNUM'] !=null)){
              $where[] = "ACTT_NUMBER_OF_PARTICIPANTS BETWEEN $list[ACTT_FNUM] AND $list[ACTT_TNUM]";                      
          }
        
          //search PERIOD START
       
       
        
        if(!empty($list['FROMDATE_SEARCH']) && !empty($list['TODATE_SEARCH'])){
            $where[] = "((ACTT_PERIOD_START BETWEEN '$list[FROMDATE_SEARCH]' AND '$list[TODATE_SEARCH]')
                         AND (ACTT_PERIOD_FINISH BETWEEN '$list[FROMDATE_SEARCH]' AND '$list[TODATE_SEARCH]'))
                        ";
            
        }else{
            if(!empty($list['FROMDATE_SEARCH'])){
            
                $where[] = "ACTT_PERIOD_START >= '$list[FROMDATE_SEARCH]'";    
            }
            if(!empty($list['TODATE_SEARCH'])){
                
                $where[] = "ACTT_PERIOD_FINISH <= '$list[TODATE_SEARCH]'";    
            }
        }
        
        
        // search LIST MFAC
        $whereMFAC="";
        for($i=1;$i<HelperBG::getIDAuto('mfac','MFAC_CODE');$i++) {
            if(isset($list['MFACC'.$i])){
                if($whereMFAC==""){
                    $whereMFAC = $i;
                }else{
                    $whereMFAC =$whereMFAC.",".$i;
                }
            }   
        }
       $MFAC_SQL=array();
       if(($whereMFAC)!=null){
            $ar=explode(',',$whereMFAC);
            foreach ($ar as $value)
                {
                    $MFAC_SQL[] = "ACTT_MFAC_CODE_LIST LIKE '%$value%'";
                }
            
        }
        if(!empty($MFAC_SQL)){
            $MFAC_SQL = implode(' OR ', $MFAC_SQL);
             $where[]="(".$MFAC_SQL.")";
        }
        
        // search LIST MOBJ
        $whereMOBJ="";
        for($i=1;$i<HelperBG::getIDAuto('mobj','MOBJ_CODE');$i++) {
            if(isset($list['MOBJJ'.$i])){
                if($whereMOBJ==""){
                    $whereMOBJ = $i;
                }else{
                    $whereMOBJ =$whereMOBJ.",".$i;
                }
            }   
        }
       $MOBJ_SQL=array();
       if(($whereMOBJ)!=null){
            $ar=explode(',',$whereMOBJ);
            foreach ($ar as $value)
                {
                    $MOBJ_SQL[] = "ACTT_MOBJ_CODE_LIST LIKE '%$value%'";
                }
            
        }
        if(!empty($MOBJ_SQL)){
            $MOBJ_SQL = implode(' OR ', $MOBJ_SQL);
             $where[]="(".$MOBJ_SQL.")";
        }
        
        // search LIST MCON
        $whereMCON="";
        for($i=1;$i<HelperBG::getIDAuto('mcon','MCON_CODE');$i++) {
            if(isset($list['MCONN'.$i])){
                if($whereMCON==""){
                    $whereMCON = $i;
                }else{
                    $whereMCON =$whereMCON.",".$i;
                }
            }   
        }
       $MCON_SQL=array();
       if(($whereMCON)!=null){
            $ar=explode(',',$whereMCON);
            foreach ($ar as $value)
                {
                    $MCON_SQL[] = "ACTT_MCON_CODE_LIST LIKE '%$value%'";
                }
            
        }
        if(!empty($MCON_SQL)){
            $MCON_SQL = implode(' OR ', $MCON_SQL);
             $where[]="(".$MCON_SQL.")";
        }
        
       if($where){
            $where = ' WHERE '.implode(' AND ', $where);
        }else{
            $where = null;
        }

        //$where .= ' AND ACTT_WEBF = 1 ';


        //get total number record
       $this->_total = $this->getTotal($where);
         
       $qr .= $where;
       
        //order by
       $qr .= " ORDER BY ACTT_DATE DESC, ACTT_CODE DESC"; 
       //echo $qr;
        //pagination
       $qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');

       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
 
       return $result;
	}

    function getTotal($where = null){
         //Load the content if it doesn't already exist
        if ($this->_total === null) {
            
            $query = "SELECT count(*)   
                                FROM actt LEFT JOIN ocba ON actt.ACTT_OCBA_CODE=ocba.OCBA_CODE
                                    	LEFT JOIN town ON ocba.OCBA_TOWN_CODE=town.TOWN_CODE
                                    	LEFT JOIN pref ON town.TOWN_PREF_CODE=pref.PREF_CODE  $where";
           //echo $query;return;
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
           
            
        }
        
        return $this->_total;
        

    } 
    
    function getPagination(){
        
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');

            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        
        return $this->_pagination;
    }
  
    //load detail leader
    function load($cid){
        $query = "SELECT  *
                    FROM actt INNER JOIN ocba ON actt.ACTT_OCBA_CODE=ocba.OCBA_CODE 
                    WHERE  ACTT_CODE= ($cid)";
/*        echo ($query);*/
        $this->_db->setQuery($query);
        $result = $this->_db->loadObject();
        //echo ($result->info_body);
        return $result;
        
    }

    //load pref
    function getAllPref(){
           $qr = "SELECT PREF_CODE,PREF_NAME FROM pref ORDER BY PREF_CODE ASC";
           $this->_db->setQuery($qr);
           $result = $this->_db->loadObjectList();
           return $result;
	}
    //load pede
    function getAllPede(){

	   $qr = "SELECT * FROM pede";
       // order by
       $qr .= " ORDER BY PEDE_CODE ASC"; 
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	} 
    
    //get all equipment
    function getAllMfac()
    {
        $qr="SELECT * FROM mfac ";
        $this->_db->setQuery($qr);
        $result=$this->_db->loadObjectList();
        return $result;
    }
     //get all object
    function getAllMobj()
    {
        $qr="SELECT * FROM mobj ";
        $this->_db->setQuery($qr);
        $result=$this->_db->loadObjectList();
        return $result;
    }
     //get all content
    function getAllMcon()
    {
        $qr="SELECT * FROM mcon ";
        $this->_db->setQuery($qr);
        $result=$this->_db->loadObjectList();
        return $result;
    }

}
?>
